import 'package:flutter_09_game/base/controller/base_list_more_controller.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/http/http_export.dart';
import 'package:flutter_09_game/pages/deposit/models/model_record_recharge.dart';
import 'package:flutter_09_game/pages/deposit/utils_deposit.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/widgets/date_picker/custom_date_picker_sheet.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import 'package:get/get.dart';

class DepositRecordRechargeLogic extends BaseListMoreController {
  RxString symbolRx = ''.obs;
  RxString timeRx = LocaleKeys.today.tr.obs;

  // 日期范围相关属性
  Rx<DatePeriod?> selectedDateRange = Rx<DatePeriod?>(null);

  //
  // List<ModelRecordRecharge> recordRechargeLists = [];

  @override
  Future<List> loadData() async {
    // 计算日期范围
    final dateRange = UtilsDeposit.calculateDateRange(timeRx.value,
        selectedDateRange: selectedDateRange.value);

    HttpResponse response = await HttpManager().get(API.getRecharge,
        params: {
          'page': pageNum,
          'size': pageSize,
          'symbol': symbolRx.value,
          'start': dateRange['start'],
          'end': dateRange['end'],
        },
        fromJson: ModelRecordRecharge.fromJson);
    // if (response.isSuccess) {
    //   recordRechargeLists = response.data;
    // }

    return response.data ?? [];
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    everAll([symbolRx], (value) {
      refreshController.requestRefresh();
    });
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  // /// 显示日期范围选择器
  // Future<void> showDateRangePicker() async {
  //   final result = await CustomDatePickerSheet.show(
  //     initialDateRange: selectedDateRange.value,
  //     firstDate: DateTime(2020),
  //     lastDate: DateTime.now(),
  //     isRangeSelection: true,
  //   );
  //
  //   if (result != null && result is DatePeriod) {
  //     selectedDateRange.value = result;
  //     timeRx.value = LocaleKeys.custom.tr;
  //     // 重新加载数据
  //     refreshList();
  //   }
  // }

  /// 更新时间选择
  void updateTimeSelection(String timeType) {

    if (timeType == LocaleKeys.custom.tr) {
      UtilsDeposit.showDateRangePicker(selectedDateRange.value).then((value) {
        if (value is DatePeriod) {
          selectedDateRange.value = value;
          refreshController.requestRefresh();
          timeRx.value = timeType;
        }
      });
    } else {
      timeRx.value = timeType;
      // 清除自定义日期范围
      selectedDateRange.value = null;
      refreshController.requestRefresh();
    }
  }
}
